package cn.javaxc.mvcwork.control.web;
import cn.javaxc.mvcwork.control.web.HtmlSelect;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.util.common.CommonUtil;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.Timestamp;
import java.text.*;
import java.util.*;
import cn.javaxc.mvcwork.controller.web.TreeBean;
import cn.javaxc.mvcwork.controller.MVCConfig;
import cn.javaxc.mvcwork.util.helper.URLHelper;
import cn.javaxc.mvcwork.controller.web.*;

public class ElementHelper{

	/**
	 * 获取时间数据中的小时
	 * @param clmn propertiesMap中的列名
	 * @return 若为无效时间,则返回-1
	 */
	public static int getHour(Map emt,String clmn){
		Object time=emt.get(clmn);
		if (time!=null){
			if (time instanceof Date){
				return ((Date)time).getHours();
			}else if (time instanceof Integer){
				Integer d0=(Integer)time;
				return d0/3600;

			}
			
		}
		return -1;
	}
	/**
	 * 获取时间数据中的分钟
	 * @param clmn propertiesMap中的列名
	 * @return 若为无效时间,则返回-1
	 */
	public static int getMinute(Map emt,String clmn){
		Object time=emt.get(clmn);
		if (time!=null){
			if (time instanceof Date){
				return ((Date)time).getMinutes();
			}else if (time instanceof Integer){
				Integer d0=(Integer)time;
				d0=d0%3600;
				return d0/60;

			}
			
		}
		return -1;
	}
	public static int getSecond(Map emt,String clmn){
		Object time=emt.get(clmn);
		if (time!=null){
			if (time instanceof Date){
				return ((Date)time).getSeconds();
			}else if (time instanceof Integer){
				Integer d0=(Integer)time;
				d0=d0%3600;
				return d0%60;

			}
			
		}
		return -1;
	}
	/**
	 * 返回时间数据中的日期String,形如 2013-01-01
	 * @param clmn propertiesMap中的列名
	 */
	public static String getDateString(Map emt,String clmn){
		if (emt.get(clmn) instanceof Timestamp){
			Timestamp time=(Timestamp)emt.get(clmn);
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(time);
		}
		return "";
	}
	public static String getTimeString(Map emt,String clmn){
		Object time=emt.get(clmn);
		if (time!=null){
			if (time instanceof Date){
				DateFormat sdf = new SimpleDateFormat("HH:mm:ss");
				return sdf.format((Date)time);
			}else if (time instanceof Integer){
				return DatabaseUtils.getDateTimeString((Integer)time);
			}else if (time instanceof Long){
				return DatabaseUtils.getDateTimeString(((Long)time).intValue());
			}			
		}
		return getString(emt,clmn);
	}

	/**
	 * 返回时间数据中的带时间的日期String,形如 2013-01-01 01:00:00
	 * @param clmn propertiesMap中的列名
	 */
	public static String getDateTimeString(Map emt,String clmn){
		Object time=emt.get(clmn);
		if (time!=null){
			if (time instanceof Date){
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return sdf.format((Date)time);
			}
			
		}
		return "";
	}
	public static boolean isEditableFile(Map emt,String clmn){
		String file=getString(emt,clmn);
		List<String> types=Arrays.asList(".doc",".xls",".ppt");
		for (String type:types ){
			if (file.endsWith(type)){
				return true;
			}
		}
		return false;
	}
	public static boolean endsWith(Map emt,String clmn,String suf){
		return getString(emt,clmn).endsWith(suf);
	}
	

  /**
   *  Gets the htmlSelect attribute of the SQLList object
   *
   * @param  selectName    Description of the Parameter
   * @param  defaultValue  Description of the Parameter
   * @return               The htmlSelect value
   */
  public static HtmlSelect getHtmlSelect(Object elist,String selectName, String defaultValue) {
    return getHtmlSelect(elist,selectName, defaultValue, false);
  }
  public static HtmlSelect getJSSelect(Object elist,String selectName, String defaultValue,String jsstr) {
	if (elist instanceof SQLList){
		((SQLList)elist).setJsEvent(jsstr);
	}
    return getHtmlSelect(elist,selectName, defaultValue, false);
  }
  /**
   *  Gets the HtmlSelect attribute of the ContactEmailTypeList object
   *
   * @param  selectName    Description of Parameter
   * @param  defaultValue  Description of Parameter
   * @param  disabled      Description of the Parameter
   * @return               The HtmlSelect value
   * @since                1.1
   */
  public static HtmlSelect getHtmlSelect(Object list,String selectName, String defaultValue, boolean checkboxOutput) {
    HtmlSelect thisSelect = new HtmlSelect();
	SQLList elist;
	if (list instanceof SQLList){
		elist=(SQLList)list;
	}else{
		return thisSelect;
	}
	if (!checkboxOutput){
		thisSelect.addItem("",elist.getSelectNone());	
	}

    thisSelect.setSelectSize(elist.getSelectSize());
    thisSelect.setSelectStyle(elist.getSelectStyle());
    thisSelect.setJsEvent(elist.getJsEvent());
    thisSelect.setCheckboxOutput(checkboxOutput);
    thisSelect.setMultiple(elist.getMultiple());
	for(Object obj:elist){
	  Map thisElement=(Map)obj;
	  thisSelect.addItem(
		  String.valueOf(thisElement.get("__code")), String.valueOf(thisElement.get("__desc")));

    }
	thisSelect.setSelectName(selectName);
	thisSelect.setDefaultValue(defaultValue);
    return thisSelect;
  }


  /**
   *  Gets the htmlSelect attribute of the SQLList object
   *
   * @param  selectName  Description of Parameter
   * @param  ms          Description of Parameter
   * @return             The htmlSelect value
   */
  public static HtmlSelect getHtmlSelect(SQLList elist,String selectName, SQLList ms) {
    HtmlSelect thisSelect = new HtmlSelect();
    thisSelect.setSelectSize(elist.getSelectSize());
    thisSelect.setSelectStyle(elist.getSelectStyle());
    thisSelect.setJsEvent(elist.getJsEvent());
    thisSelect.setMultiple(elist.getMultiple());
//    thisSelect.setMultipleSelects(ms);
    
	for(Object obj:elist){
	  Map thisElement=(Map)obj;
	  thisSelect.addItem(
		  String.valueOf(thisElement.get("__code")), String.valueOf(thisElement.get("__desc")));
    }
	thisSelect.setSelectName(selectName);
    return thisSelect;
  }
	public static String getOutlineLink(SQLList list,String link,String linkKey,Object appendurl) {
		StringBuffer buffer=new StringBuffer("<img border=\"0\" src=\""+MVCConfig.getWebStatic()+"images/icons/stock_home-16.gif\" align=\"absmiddle\">");
		for (int i=list.size()-1;i>=0;i--){
			Map emt=(Map)list.get(i);
			buffer.append("<a href=\"" + MVCConfig.getWebmenu().getJspUrl(link,true));
			if (link.indexOf("?")>0){
				buffer.append("&");
			}else{
				buffer.append("?");
			}
			buffer.append(linkKey+"=");
			buffer.append(emt.get("__code"));
			buffer.append(appendurl);
			buffer.append("\">");
			buffer.append(emt.get("__desc"));
			buffer.append("</a>\n");
			if(i!=0)buffer.append(" > ");
		}
		return buffer.toString();
	}
	public static String getSelectLink(Object list,String link,String linkKey,Object appendurl,Object select) {
		return URLHelper.getSelectLink(getHtmlSelect(list,linkKey,select+""),link,linkKey,appendurl,select);
	}
	public static HtmlSelect getHtmlSelect(Object list,String selectName, Object value,String nonesel,String jsstr) {
		if (list==null){
			list=new SQLList();
		}
		if (list instanceof SQLList){
			if (nonesel.length()>0){
			((SQLList)list).setSelectNone(nonesel);
			}
		}
		HtmlSelect sel = getHtmlSelect(list,selectName,value+"");
		sel.setJsEvent(jsstr);
		return sel;
	}
	public static HtmlSelect getHtmlRadio(Object list,String selectName, Object value,String jsstr) {
		HtmlSelect sel = getHtmlSelect(list,selectName,value+"",true);
		sel.setJsEvent(jsstr);
		sel.setCheckboxOutput(true);
		return sel;
	}
	public static HtmlSelect getHtmlCheck(Object list,String selectName, Object value,String jsstr) {
		HtmlSelect sel = getHtmlSelect(list,selectName,value+"",true);
		sel.setJsEvent(jsstr);
		sel.setCheckboxOutput(true);
		sel.setMultiple(true);
		if (value!=null){
			sel.setMultipleSelects(CommonUtil.asList((value+"").split(",")));
		}
		return sel;
	}
	public static SQLList getDataValue(SQLList _data,Object key,String kid){
		SQLList list=new SQLList();
		for(Object obj:_data){
		  Map vet=(Map)obj;
			if (String.valueOf(key).equals(String.valueOf(vet.get(kid)))){
				list.add(vet);
			}
		}
		return list;
	}
	public static TreeBean getTreebean(SQLList list){
		return list.getTreeBean();
	}
	public static String getTreeAttr(SQLList list,Map emt){
		if (list instanceof ISQLQuery){
			if (emt.get("__tree") instanceof TreeOutline){
				return " tree='row"+((TreeOutline)emt.get("__tree")).getOutAttr()+"'";
			}else{
				return "";
			}
		}
		TreeBean tb=list.getTreeBean();
		String ret=" id='"+emt.get(tb.getTreeNodeId())+"'";
		if (emt.get(tb.getTreeNodePid())!=null){
			ret+=" pid='"+emt.get(tb.getTreeNodePid())+"'";
		}
		if ("a".equals(emt.get("SON"))){
//				ret+=" hasChild=true";
		}
		return ret;
	}
	public static String getTreeNodeUrl(Map emt,TreeBean tb){
		String ret="";
		if (tb.getTableNodeUrl()!=null&&!"".equals(tb.getTableNodeUrl())){
		

			if (emt.get(tb.getTableNodeUrl())!=null) {
				return getString(emt,tb.getTableNodeUrl());
			}
		}else{
			ret=tb.getTreeNodeUrl();
			if(ret==null)ret="";
			if(ret.indexOf("?")>0)
				ret+="&";
			else
				ret+="?";
			ret+=(tb.getLinkName());
			ret+=("=");
			ret+=emt.get(tb.getTreeNodeId());
		}
		return ret;
	}
	public static String getRecordJsp(Map emt,String name){
		return "/record/"+emt.get("record_table_sid")+name+".html";
	}
	public static String getString(Map emt,String clmn){
		Object o=emt.get(clmn);
		if (o!=null){
			return o.toString();
		}
		return "";
	}
	public static boolean containsKey(Map emt,Object clmn){
		return emt.containsKey(clmn);
	}

}
